<template>
	<z-paging ref="paging" class="pages" :empty-view-center="false" :auto="false" :refresher-enabled="false" :show-scrollbar="false">
		<view class="head">
			<image class="headImg" src="/static/loginBg.png" mode="widthFix"></image>
			<view class="headLogoBox">
				<image class="pages_logo" src="/static/logo.png" mode="widfix" />
			</view>
		</view>
		<view class="pages_content">
			<uni-forms class="uniForms" ref="useForm" :modelValue="forms">
				<uni-forms-item name="name">
					<view class="pages_phone borderBottom">
						<fui-input :size="25" maxlength="11" :inputBorder="false" :borderBottom="false" placeholder="请输入手机号码" v-model="forms.accountName">
							<template v-slot:left>
								<image class="prefix" src="/static/codePhone.png" mode="widthFix"></image>
							</template>
						</fui-input>
					</view>
				</uni-forms-item>
				<uni-forms-item name="password">
					<view class="pages_phone borderBottom">
						<fui-input :size="25" maxlength="11" :type="forms.isLock ? 'password' : 'text'" :borderBottom="false" placeholder="请输入密码" v-model="forms.loginPass">
							<template v-slot:left>
								<image class="prefix" src="/static/codeImg.png" mode="widthFix"></image>
							</template>
							<template v-slot:default>
								<u-icon v-if="forms.isLock" name="eye-off" size="40" color="rgb(178, 178, 178)" @click="lockClick(false)"></u-icon>
								<u-icon v-else name="eye" size="35" color="rgb(178, 178, 178)" @click="lockClick(true)"></u-icon>
							</template>
						</fui-input>
					</view>
				</uni-forms-item>
			</uni-forms>
			<view class="register">
				<text @click="forgetThePassword">忘记密码</text>
				<text @click="navTo('/pages/logisticsCompany/register/register')">还没账号?点击注册</text>
			</view>
			<!--  -->
			<!-- btn  -->
			<button type="primary" class="loginUp" @click="login">登录</button>
			<!-- ends -->
		</view>
		<!-- {{ checked }} -->
		<agreeVue @change="checkBoxChanage" />
	</z-paging>
</template>

<script setup>
import { onLoad, onShow } from '@dcloudio/uni-app';
import { onBeforeMount, onMounted, reactive, ref, toRefs, getCurrentInstance } from 'vue';
import { storeToRefs } from 'pinia';
import http from '@/common/request.js';
import { urlToObj, sgoto, navTo, setLoadingToast, setToast, setUserIdentity } from '@/common/utils.js';
import useStore from '@/store';
import { cloneDeep } from 'lodash';
import agreeVue from '@/components/agree.vue';
import { getCode, getWxCacheNews } from '../../../common/wx';
import { getOpenidStatus, setOpenidStatus } from '../../../common/openId';

// 公司全局状态管理器
const { useLogisicsCompany, useWx } = useStore();

// state
const { userInfo } = storeToRefs(useLogisicsCompany);

const { getOpenId } = storeToRefs(useWx);

// action
const { companyLogin, getToken } = useLogisicsCompany;

// 登录表单
const forms = reactive({
	accountName: '',
	loginPass: '',
	code: '',
	isLock: false
});
// 是否同意
const checked = ref(false);

const checkBoxChanage = (d) => {
	console.log('e ====> ', d);
	checked.value = d;
};
// 状态控制
const lockClick = (lock) => {
	forms.isLock = lock;
};

// 登录
const login = () => {
	// ^1[3-9]\d{9}$
	const regxPone = /^1[3-9]\d{9}$/;
	if (!regxPone.test(forms.accountName)) {
		setToast('请填写正确的账号');
		return;
	}
	if (!forms.accountName) {
		setToast('请填写密码');
		return;
	}
	if (!checked.value) {
		setToast('请同意并勾选下方政策协议后操作~');
		return;
	}

	// 登陆
	try {
		// console.log('getOpenId =============> ', getOpenId);
		forms.openid = getOpenId;
		companyLogin(forms);
	} catch (e) {
		//TODO handle the exception
		uni.hideLoading();
	}
};

const forgetThePassword = () => {
	setToast('暂未开放');
};
//
onMounted(() => {
	setUserIdentity();
	// 判断是否已经登陆了
	if (getToken) {
		navTo('/pages/logisticsCompany/index/index');
	} else {
		getCode();
	}
});
</script>

<style scoped lang="scss">
@import './login.scss';
</style>
